import cTitle from "components/title";
import { Toast } from 'vant';
export default {
  components: { cTitle },
  data() {
    return {
      applyModel: {
        real_name: "",
        province_id: "",
        city_id: "",
        district_id: "",
        mobile: "",
        email: "",
        wechat: ""
      },
      areaValue: "",
      areaPopupVisible: false,
      area: district,
      loading: false
    };
  },

  methods: {
    /**
     * 初始化
     */
    init: function() {
      this.applyModel = {
        real_name: "",
        province_id: "",
        city_id: "",
        district_id: "",
        mobile: "",
        email: "",
        wechat: ""
      };
      this.areaValue = "";
    },
    /**
     * 地区选择完毕
     * @param {Object} obj 选中结果
     */
    addressCallback: function(obj) {
      const { itemName1, itemName2, itemName3, itemValue1, itemValue2, itemValue3 } = obj
      this.areaValue = `${itemName1}-${itemName2}-${itemName3}`;
      this.applyModel.province_id = itemValue1;
      this.applyModel.city_id = itemValue2;
      this.applyModel.district_id = itemValue3;
    },

    /**
     * 提交申请
     */
    setApplyData: function() {
      if (this.loading) {
        return;
      }
      const { real_name, province_id, city_id, district_id, mobile, email, wechat } = this.applyModel;
      if (this.fun.isTextEmpty(real_name)) {
        Toast("请填写姓名");
        return;
      }

      if (this.fun.isTextEmpty(province_id) || this.fun.isTextEmpty(city_id) || this.fun.isTextEmpty(district_id)) {
        Toast("请选择地区");
        return;
      }


      if (this.fun.isTextEmpty(mobile)) {
        Toast("请填写手机号码");
        return;
      }
      if (this.fun.isMoblie(mobile)) {
        Toast("请输入正确的手机号");
        return;
      }

      if (this.fun.isTextEmpty(email)) {
        Toast("请填写邮箱");
        return;
      }
      if (this.fun.isEmail(email, true)) {
        Toast("请输入正确的邮箱");
        return;
      }

      if (this.fun.isTextEmpty(wechat)) {
        Toast("请填写微信号");
        return;
      }

      this.loading = true;
      const json = {
        data: this.applyModel
      };

      const that = this;
      $http.post('plugin.team-partner.api.team-partner.setAgentApply', json, '提交中')
        .then(
          response => {
            if (response.result === 1) {
              that.init();
              that.$dialog.alert({ message: response.msg }).then(() => {
                that.$router.go(-1);
              });
            } else {
              Toast(response.msg || '提交失败');
            }
          },
          function (response) {
            console.log(response);
          }
        )
        .finally(() => {
          that.loading = false;
        });
    }
  }
};